import tooles from './tooles.js'

const loadingTextArr = [" w(ﾟДﾟ)w", "（づ￣3￣）づ╭❤～",
	"o(≧口≦)o", " ~(～￣▽￣)～", "(p≧w≦q)", " (*￣０￣)ノ", " o(*￣3￣)o",
	"♡加油QvQ", "♡你肯定可以的！", "♡小菜一碟~", "♡你能行!", "♡你是最棒的",
	"金榜题名♡", "心想事成♡"
]
const lan = 'zh-CN'

function random() {
	let textIndex = 0
	return textIndex = textIndex++ > loadingTextArr.length - 1 ? 0 : textIndex;
}


// 请求拦截器
// const beforeRequest = 

// // 响应拦截器
// const afterRequest = 

// // 错误回调处理(接口异常处理)
// const errorRequest = 

export default {
	beforeRequest(el) {
		el.header = {
			...el.header,
			'accept-language': lan,
			'axh-access-token': uni.getStorageSync('accesstoken')
		}

		if (!el.data || el.data.isSkipLoading !== 'true') {
			uni.showLoading({
				title: '加载中',
			})
		}

	},
	afterRequest(el, res) {
		// console.log("afterRequest", res)
		uni.hideLoading()
		if (res.data && res.data.token) {
			uni.setStorageSync('accesstoken', res.data.token)
		}

		if (res.data && res.data.code === 410) {
			uni.removeStorage({
				"key": "accesstoken"
			})
			uni.clearStorageSync();
			uni.showToast({
				title: '登录过期或该账号在其他地方登录',
				duration: 2000,
				icon: 'none'
			})
			uni.redirectTo({
				url: '/pages/login/login'
			})
		}
	},
	errorRequest(el, res) {
		uni.hideLoading()
		tooles.getAlert('服务器错误，请稍后再试！')
	}
}